$(function() {

  function layerCss(){
    $(".layui-layer").css("border-radius","10px")
    $(".layui-layer-setwin .layui-layer-close2").css({"right":"-40px","top":"0px"});
    $(".layui-layer-ico").css("background","url("+jumpUrl+"/img/question2/iconlayer.png)");
  }
    /*登录弹窗*/
  $(".loginPopup").click(function(){
    sessionStorage.setItem("goback", location.href);
    layer.closeAll();
    var Popup = $("#loginPopup");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['400px', '540px'],
          skin: 'yourclass', //样式类名
          content: Popup //内容 
    });
    layerCss();
  });
  /*律师入口*/
   //----------------------------------------------------------------------------------------
  $(document).on( "click", ".lv-entrance", function(e) {
    $(".loginPopup").click();
    sessionStorage.setItem("entrance", "entrance");
    // var entrance = sessionStorage.getItem("entrance");
    // sessionStorage.removeItem("entrance");
  });
  
  $(document).on( "click", ".layui-layer-setwin", function(e) {
    sessionStorage.removeItem("entrance");
  });

  
   /*登录提交*/
   //----------------------------------------------------------------------------------------
  $(document).on( "click", ".error-tip", function(e) {
    $(".error-tip").css("display","none");
  });

    /*登录提交*/
  $(document).on( "click", ".Popupsublogo", function(e) {
    submitForm();
    stopDefault(e);
  });

  /* 按enter提交表单 */
  document.onkeydown = function(e) {
    var e = event || window.event || arguments.callee.caller.arguments[0],
     keycode = e.which || e.keyCode;
     if (keycode==13) {
        submitForm();
        return false; 
     }else{
      return true;
     }
     stopDefault(e);
  }
  $('#username').bind('input propertychange', function(e) {
      $(".txt").hide();
      $(".error-tip").hide();
      $('.rphonecode').unbind('input propertychange');
      stopDefault(e);
  });
  $('#password').bind('input propertychange', function(e) {
      $(".txt").hide();
      $(".error-tip").hide();
      $('.rphonecode').unbind('input propertychange');
      stopDefault(e);
  });
  function submitForm(){
    var username = $("#username").val();
    var password= $("#password").val();
    // sessionStorage.setItem("goback", location.href);
    var usernames = $("#username");
    var passwords = $("#password");
    var post_id =  JSON.parse(localStorage.getItem('post_id'));
    $("#post_id").attr("value",post_id);
    if(checkusername(usernames) && checkPWd(passwords)){
      var loading = layer.msg('加载中...', {icon: 16});
      var username = $("#username").val();
      var password = $("#password").val();
      $.post(jumpUrl+"/login", {"password":password,"username":username,"post_id":post_id},function(data){
        
        // $.post("http://test.elvtuan.com/login", {"password":password,"username":username},function(data){
        //   var token = data.token;
        //   localStorage.setItem("token", JSON.stringify(token));
        // });

        // 保存token
        var token = data.token;
        localStorage.setItem("token", JSON.stringify(token));
        // 判断是否律师入口进的登录
        var entrance = sessionStorage.getItem("entrance");
        if (entrance == "entrance") {
            if (data.data.status == 1) {
              location.href=jumpUrl+"/center/become_lawyer";
            }else if (data.data.status == 3) {
              location.href=jumpUrl+"/center/lawyer/main";
            }else{
              location.href=jumpUrl;
            }
          sessionStorage.removeItem("entrance");
          return false;
        }
        
        if(post_id != null){
          var post_id_status = true;
          localStorage.setItem("post_id_status", JSON.stringify(post_id_status));
        }
        var goback = sessionStorage.getItem("goback");
        if(goback == null || goback=="" || goback=="undefined"){
          location.href=jumpUrl;
        }else{
            location.href= goback;
        }
        sessionStorage.removeItem("goback");


      }).error(function(data) {
           var obj = $.parseJSON(data.responseText);
           layer.close(loading);
           $(".error-tip").show();
           $(".error-tip").text(obj.error_messages.password);
      });
         return true;   
    }else{
      return false;
    }
  }
});
